package com.hd123.rumba.qt.def;

import com.hd123.rumba.exception.BusinessException;

// 2006-12-21 添加了对CGROUPINDEX的设置用于分组显示

public class SQLColumn {
  private QueryDefinition query;
  private String aggregation;
  private String name;
  private String title;
  private boolean visible;
  private String gAggregation;
  private boolean displayInNext;
  private boolean key;
  private String pattern = ""; // 显示格式
  private int width = 100; // 报表列宽度
  private int groupIndex = -1;

  /**
   * 返回用于select子句的列表达式
   * 
   * @return
   */
  public String getSQLSelectExpression() throws BusinessException {
    return getSQLSelectExpression(true);
  }

  /**
   * @param nullIfNotVisible
   *          true=如果此列非可见的, 返回null.
   * @return
   */
  public String getSQLSelectExpression(boolean nullIfInVisible) throws BusinessException {
    if (!isVisible() && nullIfInVisible)
      return null;
    StringBuffer sb = new StringBuffer(2);
    String s = query.translateColumn(getName());
    if (getAggregation() == null || getAggregation().equals("AGGREGATION")) {
      sb.append(s);
    } else {
      if (getAggregation().equals("COUNTDISTINCT"))
        sb.append(" count(distinct " + s + ") ");
      else
        sb.append(" " + getAggregation() + "(" + s + ") ");
    }
    return sb.toString();
  }

  public String debugInfo() {
    StringBuffer sb = new StringBuffer();
    sb.append(getClass().getName() + "\n");
    sb.append("Aggregation: " + getAggregation() + "\n");
    sb.append("Name: " + getName() + "\n");
    sb.append("Title: " + getTitle() + "\n");
    sb.append("Visible: " + isVisible() + "\n");
    sb.append("GAggregation: " + getGAggregation() + "\n");
    sb.append("DisplayInNext: " + isDisplayInNext() + "\n");
    sb.append("Key: " + isKey() + "\n");
    return sb.toString();
  }

  public String getAggregation() {
    return aggregation;
  }

  public void setAggregation(String aggregation) {
    this.aggregation = aggregation;
  }

  public boolean isDisplayInNext() {
    return displayInNext;
  }

  public void setDisplayInNext(boolean displayInNext) {
    this.displayInNext = displayInNext;
  }

  public String getGAggregation() {
    return gAggregation;
  }

  public void setGAggregation(String aggregation) {
    gAggregation = aggregation;
  }

  public boolean isKey() {
    return key;
  }

  public void setKey(boolean key) {
    this.key = key;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public boolean isVisible() {
    return visible;
  }

  public void setVisible(boolean visible) {
    this.visible = visible;
  }

  public QueryDefinition getQuery() {
    return query;
  }

  public void setQuery(QueryDefinition query) {
    this.query = query;
  }

  public String getPattern() {
    return pattern;
  }

  public void setPattern(String pattern) {
    this.pattern = pattern;
  }

  public int getWidth() {
    return width;
  }

  public void setWidth(int width) {
    this.width = width;
  }

  public int getGroupIndex() {
    return groupIndex;
  }

  public void setGroupIndex(int groupIndex) {
    this.groupIndex = groupIndex;
  }
}
